package com.itxh.core.sqlsession;

import com.itxh.core.config.Configuration;
import com.itxh.core.config.MappedStatement;
import com.itxh.core.excutor.Executor;

import java.util.List;

public class DefaultSqlSession implements SqlSession{
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <T> List<T> selectList(String statementId, Object param) {
        MappedStatement mappedStatement = configuration.getMappedStatementById(statementId);
        Executor executor = configuration.newExecutor(null);
        return executor.query(mappedStatement,configuration,param);
    }

    @Override
    public <T> T selectOne(String statementId, Object param) {
        List<Object> list = this.selectList(statementId, param);
        if(list != null && list.size() ==1){
            return (T) list.get(0);
        }
        return null;
    }

    @Override
    public int insert(String statementId, Object param) {
        return 0;
    }
}
